﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;

namespace eStore.Models
{
    [Serializable]
    public partial class eStoreUser :MarshalByRefObject, IPrincipal
    {
        public string UserName { get; set; }
        public string Role { get; set; }

        public override object InitializeLifetimeService()
        {
            return null;
        }
        
        public eStoreUser(string userName, string role){
            UserName = userName;
            Role = role;
        }

        public static eStoreUser Guest
        {
            get
            {
                return new eStoreUser("Guest", "guest");
            }
        }

        #region IPrincipal Members

        public IIdentity Identity
        {
            get
            {
                bool isAuthenticated = !(Role.Equals("guest"));
                return new Identity(isAuthenticated, this.UserName);
            }
        }

        #endregion


        public bool IsInRole(string role)
        {
            string []roles = new string[] {"admin","user","guest"};

            if (roles.Contains(role.ToLower()))
            {
                return Role.Equals(role, StringComparison.InvariantCultureIgnoreCase);
            }

            return false;
        }
    }

    public class Identity : IIdentity
    {
        bool isAuthenticated;
        string name;

        public Identity(bool isAuthenticated, string name)
        {
            this.isAuthenticated = isAuthenticated;
            this.name = name;
        }

        #region IIdentity Members

        public string AuthenticationType
        {
            get { return "Forms"; }
        }

        public bool IsAuthenticated
        {
            get { return isAuthenticated; }
        }

        public string Name
        {
            get { return name; }
        }

        #endregion
    }

}